home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.10 Oct 95 / Executing OSA Scripts from Apps / TRunOSA.cp < prev    next >
Encoding:
Text File  |  1995-05-08  |  5.2 KB  |  256 lines  |  [TEXT/MMCC]

  1. /*
  2. *    TRunOSA.cp
  3. *
  4. *    Implementation for 'TRunOSA', a C++
  5. *    class to manipulate AppleScripts
  6. *
  7. *    azn@nemeng.mpx.com.au
  8. *
  9. *    © Andrew Nemeth  Warrimoo  Australia  1995
  10. */
  11.  
  12.  
  13. #include    "TRunOSA.h"
  14.  
  15. //  #define    NDEBUG
  16. #include    <assert.h>
  17.  
  18. #include    <OSAGeneric.h>
  19. #include    <Components.h>
  20. #include    <OSA.h>
  21. #include    <AppleScript.h>
  22. #include    <AEObjects.h>
  23. #include    <Gestalt.h>
  24.  
  25.  
  26. //    Class static variable definition
  27. //
  28. ComponentInstance    TRunOSA::scriptComponent = NULL;
  29.  
  30.  
  31.  
  32.  
  33. TRunOSA::TRunOSA( )
  34.         :    f_osaidOriginal ( kOSANullScript ),
  35.             f_osaidResult ( kOSANullScript )
  36. {
  37.     f_aedescScript.descriptorType = typeNull;
  38.     f_aedescScript.dataHandle = NULL;
  39. }
  40.  
  41.  
  42.  
  43.  
  44. TRunOSA::~TRunOSA()
  45. {
  46.     if ( NULL != TRunOSA::scriptComponent )
  47.         {
  48.         ::AEDisposeDesc( &f_aedescScript );
  49.         ::OSADispose( TRunOSA::scriptComponent, f_osaidOriginal );
  50.         ::OSADispose( TRunOSA::scriptComponent, f_osaidResult );
  51.         }
  52. }
  53.  
  54.  
  55.  
  56.  
  57. OSAError    TRunOSA::initOSA( void )
  58. {
  59.     Handle        hScript = NULL;
  60.     OSAError    osaErr = noErr;
  61.  
  62.     hScript = ::Get1IndResource( typeOSAGenericStorage, 1 );
  63.     if ( NULL == hScript )
  64.         return( resNotFound );
  65.  
  66.     ::DetachResource( hScript );
  67.  
  68.     osaErr = myInitOSA( hScript );
  69.  
  70.     return( osaErr );
  71. }
  72.  
  73.  
  74.  
  75.  
  76. OSAError        TRunOSA::initOSA( const FSSpec & fsspecScript )
  77. {
  78.     Handle        hScript = NULL;
  79.     short        shResRefNum = -1;
  80.     OSAError    osaErr = noErr;
  81.  
  82.     shResRefNum = ::FSpOpenResFile( &fsspecScript, fsRdPerm );
  83.     if ( -1 == shResRefNum ) 
  84.         return( ::ResError() );
  85.  
  86.     hScript = ::Get1IndResource( typeOSAGenericStorage, 1 );
  87.     if ( NULL == hScript )
  88.         return( resNotFound );
  89.  
  90.     ::DetachResource( hScript );
  91.     ::CloseResFile( shResRefNum );
  92.  
  93.     osaErr = myInitOSA( hScript );
  94.  
  95.     return( osaErr );
  96. }
  97.  
  98.  
  99.  
  100.  
  101. OSAError        TRunOSA::myInitOSA( Handle hScript )
  102. {
  103.     const long        klgGestaltMask = 1L;
  104.     long            lgFeature = 0L;
  105.     AEDesc            aedescDummy = { typeNull, NULL };
  106.     OSErr            myErr = noErr;
  107.     OSAError        osaErr = noErr;
  108.  
  109.     assert( NULL != hScript );
  110.  
  111.     if ( NULL == TRunOSA::scriptComponent  )
  112.         {
  113.         myErr = ::Gestalt( gestaltAppleEventsAttr, &lgFeature );
  114.         if ( ( noErr == myErr ) && 
  115.             ( lgFeature & ( klgGestaltMask << gestaltScriptingSupport ) ) )
  116.             NULL;
  117.         else
  118.             return( errOSACantOpenComponent );
  119.  
  120.         TRunOSA::scriptComponent = 
  121.             ::OpenDefaultComponent( kOSAComponentType,
  122.                                         kOSAGenericScriptingComponentSubtype );
  123.  
  124.         osaErr = ::OSASetDefaultScriptingComponent( TRunOSA::scriptComponent, 
  125.                                         kAppleScriptSubtype );
  126.         if ( noErr != osaErr )
  127.             return( osaErr );
  128.         }
  129.  
  130.     ::AEDisposeDesc( &f_aedescScript );
  131.     f_aedescScript.descriptorType = typeOSAGenericStorage;
  132.     f_aedescScript.dataHandle = hScript;
  133.  
  134.     if ( kOSANullScript != f_osaidOriginal  )
  135.         ::OSADispose( TRunOSA::scriptComponent, f_osaidOriginal );
  136.  
  137.     osaErr = ::OSALoad( TRunOSA::scriptComponent, &f_aedescScript, 
  138.                     kOSAModeNull, &f_osaidOriginal );
  139.  
  140.     ::OSADisplay( TRunOSA::scriptComponent, f_osaidOriginal,
  141.                 typeChar, kOSAModeDisplayForHumans,
  142.                 &aedescDummy );
  143.  
  144.     ::AEDisposeDesc( &aedescDummy );
  145.  
  146.     return( osaErr );
  147. }
  148.  
  149.  
  150.  
  151.  
  152. OSAError        TRunOSA::runScript( void )
  153. {
  154.     if ( NULL == TRunOSA::scriptComponent  )
  155.         return( errOSAInvalidID );
  156.  
  157.     if ( kOSANullScript != f_osaidResult  )
  158.         ::OSADispose( TRunOSA::scriptComponent, f_osaidResult );
  159.  
  160.     return( ::OSAExecute( TRunOSA::scriptComponent, f_osaidOriginal, 
  161.                 kOSANullScript, kOSAModeNull, &f_osaidResult ) );
  162. }
  163.  
  164.  
  165.  
  166.  
  167. OSAError    TRunOSA::runScript( const AEEventClass        aeclassSuite, 
  168.                                 const AEEventID         aesuiteKind,
  169.                                 Str255                     str255Var )
  170. {
  171.     AEDesc        aedescVar    = { typeNull, NULL };
  172.     OSAError    osaErr    = noErr;
  173.  
  174.     assert( str255Var[0] > 0 );
  175.  
  176.     osaErr = ::AECreateDesc( typeChar, &str255Var[1], 
  177.                         str255Var[0], &aedescVar );
  178.  
  179.     if ( noErr == osaErr )
  180.         osaErr = myRunScript( aeclassSuite, aesuiteKind, &aedescVar );
  181.  
  182.     ::AEDisposeDesc( &aedescVar );
  183.  
  184.     return( osaErr );
  185. }
  186.  
  187.  
  188.  
  189.  
  190. OSAError        TRunOSA::myRunScript( const AEEventClass    aeclassSuite, 
  191.                                     const AEEventID         aesuiteKind,
  192.                                     AEDesc                     * ptraedescVar )
  193. {
  194.     AppleEvent    aeEvent    = { typeNull, NULL };
  195.     OSAError    osaErr    = noErr;
  196.  
  197.     if ( NULL == TRunOSA::scriptComponent )
  198.         return( errOSAInvalidID );
  199.  
  200.     assert( aeclassSuite > 0L );
  201.     assert( aesuiteKind > 0L );
  202.  
  203.     osaErr = ::AECreateAppleEvent( aeclassSuite,
  204.                             aesuiteKind,
  205.                             &f_aedescScript,
  206.                             kAutoGenerateReturnID,
  207.                             kAnyTransactionID,
  208.                             &aeEvent );
  209.  
  210.     if ( NULL != ptraedescVar && noErr == osaErr )
  211.         osaErr = ::AEPutParamDesc( &aeEvent, keyDirectObject, ptraedescVar );
  212.  
  213.     if ( noErr == osaErr && ( kOSANullScript != f_osaidResult ) )
  214.         ::OSADispose( TRunOSA::scriptComponent, f_osaidResult );
  215.  
  216.     if ( noErr == osaErr )
  217.         osaErr = ::OSAExecuteEvent( TRunOSA::scriptComponent, 
  218.                                 &aeEvent, f_osaidOriginal, 
  219.                                 kOSAModeNull, &f_osaidResult );
  220.  
  221.     ::AEDisposeDesc( &aeEvent );
  222.  
  223.     return( osaErr );
  224. }
  225.  
  226.  
  227.  
  228.  
  229. OSAError        TRunOSA::getResultDesc( AEDesc     * ptraedescResult,
  230.                                         long    * ptrlgSize )
  231. {
  232.     OSAError    osaErr = noErr;
  233.  
  234.     if ( NULL == TRunOSA::scriptComponent && 
  235.         kOSANullScript  == f_osaidResult )
  236.         return( errOSAInvalidID );
  237.  
  238.     assert( NULL != ptraedescResult );    
  239.     assert( NULL != ptrlgSize );    
  240.  
  241.     osaErr = ::OSADisplay( TRunOSA::scriptComponent, f_osaidResult,
  242.                         typeChar, kOSAModeDisplayForHumans,
  243.                         ptraedescResult );
  244.  
  245.     if ( noErr == osaErr )
  246.         *ptrlgSize = ::GetHandleSize( ptraedescResult->dataHandle );
  247.     else
  248.         *ptrlgSize = 0L;
  249.  
  250.     ::OSADispose( TRunOSA::scriptComponent, f_osaidResult );
  251.     f_osaidResult = kOSANullScript;
  252.  
  253.     return( osaErr );
  254. }
  255.  
  256.